const config = require('../config/config');
const dataUrl = config.testServer;

const axios = require("axios");
const qs = require('qs');
module.exports = {
    getAllMachineInfos,
    getMachineInfosBySerialNumber,
    initMachineInfos,
    updateMachineInfos,
    deleteMachineInfos,
    updateEquipmentHistory
}

async function getAllMachineInfos(payload) {
    let url = dataUrl + 'api/v1/data/getAllMachineInfos'
    let data = qs.stringify({
        unionId: payload.unionId,
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}



async function getMachineInfosBySerialNumber(payload) {
    let url = dataUrl + 'api/v1/data/getMachineInfosBySerialNumber'
    let data = qs.stringify({
        unionId: payload.unionId,
        serialNumber: payload.serialNumber
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}




async function initMachineInfos(payload) {
    let url = dataUrl + 'api/v1/data/initMachineInfos'
    let data = qs.stringify({
        unionId: payload.unionId,
        serialNumber: payload.serialNumber,
        machineType: payload.machineType,
        machineInfo: payload.machineInfo,
        equipmentHistory: payload.equipmentHistory
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}




async function updateMachineInfos(payload) {
    let url = dataUrl + 'api/v1/data/updateMachineInfos'
    let data = qs.stringify({
        unionId: payload.unionId,
        serialNumber: payload.serialNumber,
        machineType: payload.machineType,
        machineInfo: payload.machineInfo,
        equipmentHistory: payload.equipmentHistory
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}




async function deleteMachineInfos(payload) {
    let url = dataUrl + 'api/v1/data/deleteMachineInfos'
    let data = qs.stringify({
        unionId: payload.unionId,
        serialNumber: payload.serialNumber,
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}


async function updateEquipmentHistory(payload) {
    let url = dataUrl + 'api/v1/data/updateEquipmentHistory'
    let data = qs.stringify({
        unionId: payload.unionId,
        serialNumber: payload.serialNumber,
        equipmentHistory: payload.equipmentHistory
    })
    let config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    };
    try {
        const result = await axios.request(config)
        return result.data
    } catch (error) {
        return error
    }
}

